package com.java2404.micro.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.java2404.micro.common.dto.GoodsDTO;
import com.java2404.micro.common.dto.OrderDTO;
import com.java2404.micro.common.entity.Merchant;
import com.java2404.micro.common.entity.Order;
import com.java2404.micro.common.entity.PageBean;
import com.java2404.micro.common.entity.User;
import com.java2404.micro.common.resp.Result;
import com.java2404.micro.order.mapper.OrderMapper;
import com.java2404.micro.order.openfeign.MerchantService;
import com.java2404.micro.order.openfeign.UserService;
import com.java2404.micro.order.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @User userlq
 * @Date 2025/1/5 19:07
 * @Classname OrderServiceImpl
 * @Description 订单实现类
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    @Resource
    private OrderMapper orderMapper;

    @Resource
    private UserService userService;

    @Resource
    private MerchantService merchantService;

    @Override
    public PageBean<OrderDTO> showAllOrders(Integer pageNum, Integer pageSize) {
        // 设置分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        // 添加查询条件，deleted = 0
        orderQueryWrapper.eq("deleted", 0);
        return getOrderDTOPageBean(orderQueryWrapper);
    }

    @Override
    public PageBean<OrderDTO> showAllOrdersBymid(Integer pageNum, Integer pageSize, Integer mid) {
        // 设置分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        // 添加查询条件0
        orderQueryWrapper.eq("deleted", 0);
        orderQueryWrapper.eq("mid", mid);

        return getOrderDTOPageBean(orderQueryWrapper);
    }

    @Override
    public PageBean<OrderDTO> showAllOrdersByuid(Integer pageNum, Integer pageSize, Integer uid) {
        // 设置分页
        PageHelper.startPage(pageNum, pageSize);
        // 查询数据
        QueryWrapper<Order> orderQueryWrapper = new QueryWrapper<>();
        // 添加查询条件
        orderQueryWrapper.eq("deleted", 0);
        orderQueryWrapper.eq("uid", uid);

        return getOrderDTOPageBean(orderQueryWrapper);
    }

    @Override
    public Result<PageBean<GoodsDTO>> ShowGoods(int i, int i1) {
        return merchantService.ShowGoods(i, i1);
    }

    /**
     * 获取分页列表
     *
     * @param orderQueryWrapper 查询条件
     * @return PageBean<OrderDTO> 分页列表
     */
    private PageBean<OrderDTO> getOrderDTOPageBean(QueryWrapper<Order> orderQueryWrapper) {
        // 设置返回数据
        PageBean<OrderDTO> orderPageBean = new PageBean<>();
        // 根据条件查询数据
        List<Order> orderList = orderMapper.selectList(orderQueryWrapper);
        // 构建分页列表
        Page<Order> page = (Page<Order>) orderList;
        // 填充PageBean的分页信息
        orderPageBean.setTotal(page.getTotal());
        // 创建订单DTO列表
        List<OrderDTO> orderDTOList = new ArrayList<>();
        // 遍历订单列表，填充DTO信息
        for (Order order : orderList) {
            OrderDTO orderDTO = new OrderDTO();
            orderDTO.setOid(order.getOid());
            orderDTO.setUid(order.getUid());
            // 通过用户id查询用户名称
            Result<User> userById = userService.getUserById(order.getUid());
            orderDTO.setUname(userById.getData().getUname());
            orderDTO.setMid(order.getMid());
            // 通过商家id查询商家名称
            Result<Merchant> merchant = merchantService.getMerchant(order.getMid());
            orderDTO.setMname(merchant.getData().getMname());
            orderDTO.setOno(order.getOno());
            orderDTO.setGtype(order.getGtype());
            orderDTO.setGid(order.getGid());
            orderDTO.setGname(order.getGname());
            orderDTO.setGprice(order.getGprice());
            orderDTO.setGstatus(order.getGstatus());
            orderDTO.setCreate_by(order.getCreate_by());
            orderDTO.setCreate_time(order.getCreate_time());
            orderDTO.setUpdate_by(order.getUpdate_by());
            orderDTO.setUpdate_time(order.getUpdate_time());
            orderDTO.setDeleted(order.getDeleted());
            orderDTOList.add(orderDTO);
        }
        orderPageBean.setItems(orderDTOList);
        return orderPageBean;
    }
}
